package com.athuahua.service;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
 * @Author: huahua
 * @Date: 2024/8/17 0017
 * @Time: 6:41
 * @Description:
 */
@Service
@Slf4j
public class AsyncService {

    @Async("mailThreadPool") //分配线程池名称
    @SneakyThrows //简化异常抛出
    public void sendMail(String email, String message){
        Thread.sleep(new Random().nextInt(3000));
        log.info("Sending email to {} with message {}", email, message);
    }


    /**
     * 返回线程执行结果
     * @param email
     * @param message
     * @return
     */
    @Async("mailThreadPool") //分配线程池名称
    @SneakyThrows //简化异常抛出
    public CompletableFuture<String> blockSendMail(String email, String message){
        Thread.sleep(new Random().nextInt(3000));
        log.info("Sending email to {} with message {}", email, message);
        return CompletableFuture.completedFuture("Email sent successfully" + email + message);
    }
}
